package view_api

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

func AddUserIndex(c *gin.Context){
	var userIndex model.UserIndex
	_ = c.ShouldBindJSON(&userIndex)

	if err := service.CreateUserIndex(userIndex); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}


func UpdateUserIndex(c *gin.Context) {
	var userIndex model.UserIndex
	_ = c.ShouldBindJSON(&userIndex)
	userIndex.Status = 1
	for k,_:= range userIndex.UserIndexTop {
		userIndex.UserIndexTop[k].ID = 0
	}
	for k,_:= range userIndex.UserIndexIntro {
		userIndex.UserIndexIntro[k].ID = 0
	}
	service.DeleteUserIndexTopByUserId(int(userIndex.ID))
	service.DeleteUserIndexIntroByUserId(int(userIndex.ID))
	if err := service.UpdateUserIndex(userIndex); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func GetUserIndex(c *gin.Context){
	UserId  := c.Query(`userId`)
	id,_ := strconv.Atoi(UserId)
	_,data := service.GetUserIndexWithUserId(id)
	if data.ID == 0 {
		data.Cname = data.Users.Name
	}
	response.OkWithData(data,c)
}